//
//  entrainTableViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-10.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "entrainTableViewController.h"
#import "videoTableViewCell.h"
#import "movieHandel.h"
#import "movieDataModel.h"

#import "UIImage+ImageDown.h"
#import "videoDetailViewController.h"
#import "videorootViewController.h"

#import "MJRefresh.h"
@interface entrainTableViewController ()//<UITableViewDragLoadDelegate>

@end

@implementation entrainTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
 
    //注册cell
    
    [self.tableView registerClass:[videoTableViewCell class] forCellReuseIdentifier:@"cell_id"];
    
    //请求数据
   
    [movieHandel movieshareData].sdr1 = 0;
 
    [[movieHandel movieshareData] requestentrainDataWithString:kentertainmentString  key:@"V9LG4CHOR"];
    
    //添加kvo
    [[movieHandel movieshareData] addObserver:self forKeyPath:@"entrainArray" options:    NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld  context:nil];
    

    [self.tableView addHeaderWithTarget:self action:@selector(dragTableDidTriggerRefresh)];
    
    [self.tableView addFooterWithTarget:self action:@selector(dragTableDidTriggerLoadMore)];
    
    
}

#pragma mark 响应观察者
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    
    
    if ([keyPath isEqualToString:@"entrainArray"]) {
   
      [self.tableView reloadData];
   }
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
      return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [[movieHandel movieshareData].entrainArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    videoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id" forIndexPath:indexPath];
    
    
    movieDataModel *mda = [movieHandel movieshareData].entrainArray[indexPath.row];
    cell.movieView.image = [UIImage imageWithUrlString:mda.cover myBlock:^(UIImage *image) {
        cell.movieView.image = image;
        
    }];
    //字符串替代
    NSString *sr = [mda.title stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    cell.titlabel.text = sr;
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //给详情接口传入vid
    videoDetailViewController *videotail = [[[videoDetailViewController alloc] init] autorelease];
    
    movieDataModel *m = [movieHandel movieshareData].entrainArray[indexPath.row];
    [movieHandel movieshareData].str = m.vid;
    videotail.str_url = m.mp4_url;
    videotail.videoTitle = m.title;
     videotail.appendString = @"recommend";
    
    //模态跳入详情页面
    videotail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    
    videorootViewController *vrc = (videorootViewController *)self.nextResponder.nextResponder.nextResponder;
    [vrc presentViewController:videotail animated:YES completion:nil];


}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 180;
    
}

//下拉刷新
#pragma mark 下拉刷新
- (void)dragTableDidTriggerRefresh{
    
    
    //请求数据
 [[movieHandel movieshareData] requestentrainDataWithString:kentertainmentString  key:@"V9LG4CHOR"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tableView reloadData];
        [self.tableView  headerEndRefreshing];
    });
    
    
    
}

//加载更多

#pragma mark 上拉加载
- (void)dragTableDidTriggerLoadMore{
    
    
    [movieHandel movieshareData].sdr1 += 10;
    
    [[movieHandel movieshareData] requestentrainDataWithString:kentertainmentString  key:@"V9LG4CHOR"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.tableView reloadData];
        [self.tableView  footerEndRefreshing];
    });
    
    
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
